package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.pojo.Activityesarea;
import com.example.demo.service.ActivityesareaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
//@RequestMapping("/activityesarea")
@Api(value = "活动管理页面",tags = "活动管理页面")
public class ActivityesareaController {
    @Autowired
    private ActivityesareaService activityesareaService;
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    //添加活动种类                               //活动名称        //折扣
    @ApiOperation(value = "添加活动",notes ="添加活动" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityesName",value ="活动名字" ),
            @ApiImplicitParam(name = "discount",value ="折扣" )
    })

 public String insertActivityesarea(String activityesName,int discount,Model model){
     Activityesarea activityesarea=new Activityesarea();
     activityesarea.setActivityesname(activityesName);
     activityesarea.setDiscount(discount);

     int flag=activityesareaService.insertSelective(activityesarea);
     if (flag==1){
          model.addAttribute("tianjia","添加成功");
     }else {
         model.addAttribute("tinajia","添加失败");
     }
     select(model);
     return "newsType";
 }

 //修改活动种类
    @RequestMapping(value = "/update",method = RequestMethod.POST)
 @ApiOperation(value = "修改活动",notes ="修改活动" )
 @ApiImplicitParams({
         @ApiImplicitParam(name = "activityesName",value ="活动名字" ),
         @ApiImplicitParam(name = "discount",value ="折扣" ),
         @ApiImplicitParam(name = "activityesAreaId",value ="活动id" ),
 })
    public String updateActivityesarea(String activityesName,int discount,int activityesAreaId){
        Activityesarea activityesarea=new Activityesarea();
        activityesarea.setDiscount(discount);
        activityesarea.setActivityesareaid(activityesAreaId);
        activityesarea.setActivityesname(activityesName);
        int flag=activityesareaService.updateByPrimaryKey(activityesarea);
        if (flag==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    //删除活动种类
    @RequestMapping(value = "/delect",method = RequestMethod.GET)
    @ApiOperation(value = "删除活动",notes ="删除活动" )
    @ApiImplicitParam(name = "activityesAreaId",value ="活动id" )
    public String delectById(int activityesAreaId,Model model){
        System.out.println(activityesAreaId);
      int flag=activityesareaService.deleteByPrimaryKey(activityesAreaId);
      if (flag==1){
          model.addAttribute("shanchu","删除成功");
      }else {
          model.addAttribute("shanchu","删除失败");
      }
      select(model);
      return "newsType";
    }

    //查找所有活动
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    @ApiOperation(value = "显示所有活动",notes ="显示所有活动" )
    public String select(Model model){
       List<Activityesarea> activityesareas=activityesareaService.selectAll();
       model.addAttribute("actityAll",activityesareas);
        model.addAttribute("changdu",activityesareas.size());
           return "newsType";
    }
}
